************************************ Merge Econ data with bartiks *************************************************************************
clear all
set more off

use econ_data, clear

merge 1:1 state year using bartik_data_compl_6
drop _merge 

merge 1:1 state year using bartik_data_compl_12
drop _merge 

merge 1:1 state year using bartik_data_compl_18
drop _merge 

merge 1:1 state year using bartik_data_compl_24
drop _merge 

merge 1:1 state year using bartik_data_compl_30
drop _merge 

merge 1:1 state year using bartik_data_compl_36
drop _merge 

merge 1:1 state year using bartik_data_compl_42
drop _merge

merge 1:1 state year using bartik_data_compl_48
drop _merge 

merge 1:1 state year using bartik_data_compl_42_soc
drop _merge

merge 1:1 state year using bartik_data_compl_42_econ
drop _merge

merge 1:1 state year using bartik_data_compl_42_fisc
drop _merge

merge 1:1 state year using bartik_data_compl_42_misc
drop _merge

merge 1:1 state year using bartik_data_compl_42_proc
drop _merge

egen statenumb=group(statename)

merge m:1 statenumb using data_control //generating statenumb to use same order as data_control 
drop _merge
drop statenumb

merge m:1 statename using sector-econ //using statename as sector data does not include state-abbrev 
drop _merge

*********************************** Final Merges ***********************************

sort state year

foreach x in provisions_6 provisions_12 provisions_18 provisions_24 provisions_30 provisions_36 provisions_42 provisions_48 word_count_6 word_count_12 word_count_18 word_count_24 word_count_30 word_count_36 word_count_42 word_count_48 cont_prov_6 cont_prov_12 cont_prov_18 cont_prov_24 cont_prov_30 cont_prov_36 cont_prov_42 cont_prov_48 non_cont_prov_6 non_cont_prov_12 non_cont_prov_18 non_cont_prov_24 non_cont_prov_30 non_cont_prov_36 non_cont_prov_42 non_cont_prov_48 cond_list_prov_6 non_cond_list_prov_6 cond_list_prov_12 non_cond_list_prov_12 cond_list_prov_18 non_cond_list_prov_18 cond_list_prov_24 non_cond_list_prov_24 cond_list_prov_30 non_cond_list_prov_30 cond_list_prov_36 non_cond_list_prov_36 cond_list_prov_42 non_cond_list_prov_42 cond_list_prov_48 non_cond_list_prov_48 cond_prov_6 non_cond_prov_6 cond_prov_12 non_cond_prov_12 cond_prov_18 non_cond_prov_18 cond_prov_24 non_cond_prov_24 cond_prov_30 non_cond_prov_30 cond_prov_36 non_cond_prov_36 cond_prov_42 non_cond_prov_42 cond_prov_48 non_cond_prov_48 constr_obl_6 constr_obl_12 constr_obl_18 constr_obl_24 constr_obl_30 constr_obl_36 constr_obl_42 constr_obl_48 permis_entit_6 permis_entit_12 permis_entit_18 permis_entit_24 permis_entit_30 permis_entit_36 permis_entit_42 permis_entit_48 provisions_42soc provisions_42econ provisions_42misc provisions_42fisc provisions_42proc cond_list_42econ non_cond_list_42econ cond_list_42fisc non_cond_list_42fisc cond_list_42proc non_cond_list_42proc cond_list_42soc non_cond_list_42soc {

gen log_`x' = log(`x')
gen `x'_D = D.log_`x'
gen F_`x'_D = F.`x'_D
gen L_`x'_D = L.`x'_D
gen L2_`x'_D = L2.`x'_D

}
save bartik_shocks_final, replace

********************************** Preparing Other Data ***********************************

**** Previous Provisions etc
 preserve
 
 foreach x in 6 12 18 24 30 36 42 48 {


 sort state year
 bysort state: gen log_prov_prev5_`x' = (log_provisions_`x'[_n-1] + log_provisions_`x'[_n-2] + log_provisions_`x'[_n-3] + log_provisions_`x'[_n-4] + log_provisions_`x'[_n-5])/5
 bysort state: gen log_word_prev5_`x' = (log_word_count_`x'[_n-1] + log_word_count_`x'[_n-2] + log_word_count_`x'[_n-3] + log_word_count_`x'[_n-4] + log_word_count_`x'[_n-5])/5
 bysort state: gen log_cont_prov_prev5_`x' = (log_cont_prov_`x'[_n-1] + log_cont_prov_`x'[_n-2] + log_cont_prov_`x'[_n-3] + log_cont_prov_`x'[_n-4] + log_cont_prov_`x'[_n-5])/5
 bysort state: gen log_non_cont_prov_prev5_`x' = (log_non_cont_prov_`x'[_n-1] + log_non_cont_prov_`x'[_n-2] + log_non_cont_prov_`x'[_n-3] + log_non_cont_prov_`x'[_n-4] + log_non_cont_prov_`x'[_n-5])/5
 bysort state: gen log_cond_list_prov_prev5_`x' = (log_cond_list_prov_`x'[_n-1] + log_cond_list_prov_`x'[_n-2] + log_cond_list_prov_`x'[_n-3] + log_cond_list_prov_`x'[_n-4] + log_cond_list_prov_`x'[_n-5])/5
 bysort state: gen log_non_cond_list_prov_prev5_`x' = (log_non_cond_list_prov_`x'[_n-1] + log_non_cond_list_prov_`x'[_n-2] + log_non_cond_list_prov_`x'[_n-3] + log_non_cond_list_prov_`x'[_n-4] + log_non_cond_list_prov_`x'[_n-5])/5
 bysort state: gen log_cond_prov_prev5_`x' = (log_cond_prov_`x'[_n-1] + log_cond_prov_`x'[_n-2] + log_cond_prov_`x'[_n-3] + log_cond_prov_`x'[_n-4] + log_cond_prov_`x'[_n-5])/5
 bysort state: gen log_non_cond_prov_prev5_`x' = (log_non_cond_prov_`x'[_n-1] + log_non_cond_prov_`x'[_n-2] + log_non_cond_prov_`x'[_n-3] + log_non_cond_prov_`x'[_n-4] + log_non_cond_prov_`x'[_n-5])/5
 bysort state: gen log_obl_prev5_`x' = (log_constr_obl_`x'[_n-1] + log_constr_obl_`x'[_n-2] + log_constr_obl_`x'[_n-3] + log_constr_obl_`x'[_n-4] + log_constr_obl_`x'[_n-5])/5
 bysort state: gen log_perm_prev5_`x' = (log_permis_entit_`x'[_n-1] + log_permis_entit_`x'[_n-2] + log_permis_entit_`x'[_n-3] + log_permis_entit_`x'[_n-4] + log_permis_entit_`x'[_n-5])/5
 }
 
 rename stateabre state_abbrev
 keep state_abbrev year log_prov_prev5_* log_word_prev5_* log_cont_prov_prev5_* log_non_cont_prov_prev5_* log_cond_list_prov_prev5_* log_non_cond_list_prov_prev5_*  log_cond_prov_prev5_* log_non_cond_prov_prev5_* log_obl_prev5_* log_perm_prev5_*
 save previous_results.dta, replace
 restore
